package com.cxlin.cwanandroid.ui.home.viewmodel

import com.cxlin.base.model.api.Result
import androidx.lifecycle.viewModelScope
import com.cxlin.base.mvi.UiState
import com.cxlin.cwanandroid.model.bean.SystemParent
import com.cxlin.cwanandroid.model.repository.CollectRepository
import com.cxlin.cwanandroid.model.repository.SystemRepository
import kotlinx.coroutines.launch
import com.cxlin.base.BaseViewModel
import com.cxlin.base.mvi.UiEvent
import com.cxlin.base.mvi.UiIntent
import kotlinx.coroutines.flow.update

/**
 *
 * @Author: chenxunlin
 * @Date: 2024/4/30 15:03
 */
class SystemViewModel(
    val systemRepository: SystemRepository,
    val collectRepository: CollectRepository
) : BaseViewModel() {

    sealed class SystemUiState : UiState {
        object Init : SystemUiState()
        data class IsRefresh(val isRefresh: Boolean) : SystemUiState()
        data class FetchSystemParents(val systemParents: List<SystemParent>) : SystemUiState()
    }

    sealed class SystemEvent : UiEvent {
        class ToastEvent(val message: String, val short: Boolean = false) : SystemEvent()
    }

    sealed class SystemUiIntent : UiIntent {
        class GetSystemTypes(val isRefresh: Boolean = false) : SystemUiIntent()
        data class CollectArticle(val articleId: Int, val boolean: Boolean) :
            SystemUiIntent()
    }

    override fun handleIntent(intent: UiIntent) {
        when(intent){
            is SystemUiIntent.GetSystemTypes ->{
                viewModelScope.launch {
                    kotlin.runCatching {
                        if (intent.isRefresh) {
                            mUiStateFlow.update {
                                SystemUiState.IsRefresh(true)
                            }
                        }
                        val result = systemRepository.getSystemTypes()
                        if(result is Result.Success){
                            mUiStateFlow.update {
                                SystemUiState.IsRefresh(false)
                            }
                            mUiStateFlow.update {
                                SystemUiState.FetchSystemParents(result.data)
                            }
                        }else if(result is Result.Error){
                            mUiStateFlow.update {
                                SystemUiState.IsRefresh(false)
                            }
                            mEffectFlow.emit(
                                SystemEvent.ToastEvent(
                                    result.exception.message ?: "网络异常",
                                    true
                                ),
                            )

                        }
                    }
                }
            }
            is SystemUiIntent.CollectArticle ->{
                viewModelScope.launch {
                    kotlin.runCatching {
                        if(intent.boolean){
                            collectRepository.collectArticle(intent.articleId)
                        }else{
                            collectRepository.unCollectArticle(intent.articleId)
                        }

                    }
                }
            }
        }
    }

    override fun createInitState(): UiState {
        return SystemUiState.Init
    }
}